package com.mao.util;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import org.apache.log4j.Logger;

import com.mao.model.common.ClassBase;
import com.mao.model.organization.OrganizationClass;
import com.mao.model.product.ProductClass;
import com.mao.model.warehouse.WareHouseClass;
import com.mao.service.organization.IOrganizationClassService;
import com.mao.service.product.IProductClassService;
import com.mao.service.warehouse.IWareHouseClassService;

public class ClassUtil {

	static Logger log = Logger.getLogger(ClassUtil.class);

	// 递归，获取商品类别的父亲的父亲，直至最顶级。
	@SuppressWarnings("unchecked")
	public static List<String> getTreePathList(ClassBase parent,
			List<String> treePathList) {
		String treePath = "";
		treePath = getSixTreePath(treePath, parent.getId());
		treePathList.add(treePath);
		@SuppressWarnings("rawtypes")
		Class cb = parent.getClass();
		if (cb.isAssignableFrom(ProductClass.class)) {
			ProductClass pc = (ProductClass) parent;
			if (pc.getParent() != null) {
				getTreePathList(pc.getParent(), treePathList);
			}
		}
		if (cb.isAssignableFrom(OrganizationClass.class)) {
			OrganizationClass oc = (OrganizationClass) parent;
			if (oc.getParent() != null) {
				getTreePathList(oc.getParent(), treePathList);
			}
		}
		if (cb.isAssignableFrom(WareHouseClass.class)) {
			WareHouseClass wc = (WareHouseClass) parent;
			if (wc.getParent() != null) {
				getTreePathList(wc.getParent(), treePathList);
			}
		}
		return treePathList;
	}

	// 获取父级的id，不满6位的前面补0
	public static String getSixTreePath(String treePath, Long parentId) {
		for (int i = String.valueOf(parentId).length(); i < 6; i++) {
			treePath += "0";
		}
		treePath += parentId;
		return treePath;
	}

	// (一级分类)获取父级的id，不满6位的前面补0
	public static String getSixTreePath(long pcId) {
		String treePath = "";
		for (int i = String.valueOf(pcId).length(); i < 6; i++) {
			treePath += "0";
		}
		treePath += pcId;
		return treePath;
	}

	// 获取Level值
	public static Integer getLevel(String treePath) {
		return treePath.split("-").length;
	}

	// 获取TreePath
	@SuppressWarnings("unchecked")
	public static String getTreePath(ClassBase classBase) {
		List<String> treePathList = null;
		@SuppressWarnings("rawtypes")
		Class cb = classBase.getClass();
		if (cb.isAssignableFrom(ProductClass.class)) {
			ProductClass pc = (ProductClass) classBase;
			// 一级分类
			if (pc.getParent() == null) {
				return getSixTreePath(pc.getId());
			} else { // 下级分类
				String treePath = "";
				treePathList = new ArrayList<String>();
				treePathList = ClassUtil.getTreePathList(pc.getParent(),
						treePathList);
				Collections.reverse(treePathList);// 倒序
				for (int i = 0; i < treePathList.size(); i++) {
					treePath += treePathList.get(i) + "-";
				}
				String treePath2 = "";
				treePath2 = ClassUtil.getSixTreePath(treePath2, pc.getId());
				treePath += treePath2;
				return treePath;
			}
		}
		if (cb.isAssignableFrom(OrganizationClass.class)) {
			OrganizationClass oc = (OrganizationClass) classBase;
			// 一级分类
			if (oc.getParent() == null) {
				return getSixTreePath(oc.getId());
			} else { // 下级分类
				String treePath = "";
				treePathList = new ArrayList<String>();
				treePathList = ClassUtil.getTreePathList(oc.getParent(),
						treePathList);
				Collections.reverse(treePathList);// 倒序
				for (int i = 0; i < treePathList.size(); i++) {
					treePath += treePathList.get(i) + "-";
				}
				String treePath2 = "";
				treePath2 = ClassUtil.getSixTreePath(treePath2, oc.getId());
				treePath += treePath2;
				return treePath;
			}
		}
		if (cb.isAssignableFrom(WareHouseClass.class)) {
			WareHouseClass wc = (WareHouseClass) classBase;
			// 一级分类
			if (wc.getParent() == null) {
				return getSixTreePath(wc.getId());
			} else { // 下级分类
				String treePath = "";
				treePathList = new ArrayList<String>();
				treePathList = ClassUtil.getTreePathList(wc.getParent(),
						treePathList);
				Collections.reverse(treePathList);// 倒序
				for (int i = 0; i < treePathList.size(); i++) {
					treePath += treePathList.get(i) + "-";
				}
				String treePath2 = "";
				treePath2 = ClassUtil.getSixTreePath(treePath2, wc.getId());
				treePath += treePath2;
				return treePath;
			}
		}
		return null;
	}

	// 更新所有下级分类的TreePath和Level 商品分类
	public static void updateTreePathAndLevel(ClassBase classBase,
			IProductClassService productClassService) {
		Class cb = classBase.getClass();
		ProductClass pc = (ProductClass) classBase;
		if (pc.getLowClass().size() > 0) {
			Set<ProductClass> pcSets = pc.getLowClass();
			Iterator<ProductClass> it = pcSets.iterator();
			while (it.hasNext()) {
				ProductClass updatePc = it.next();
				updatePc.setTreePath(getTreePath(updatePc)); // 更新TreePath
				updatePc.setLevel(getLevel(updatePc.getTreePath())); // 更新Level
				productClassService.updateProductClass(updatePc);
				if (updatePc.getLowClass().size() > 0) {
					updateTreePathAndLevel(updatePc, productClassService);
				}
			}
		}
	}

	// 更新所有下级分类的TreePath和Level 往来单位分类
	public static void updateTreePathAndLevel(ClassBase classBase,
			IOrganizationClassService organizationClassService) {
		Class cb = classBase.getClass();
		OrganizationClass oc = (OrganizationClass) classBase;
		if (oc.getLowClass().size() > 0) {
			Set<OrganizationClass> ocSets = oc.getLowClass();
			Iterator<OrganizationClass> it = ocSets.iterator();
			while (it.hasNext()) {
				OrganizationClass updatePc = it.next();
				updatePc.setTreePath(getTreePath(updatePc)); // 更新TreePath
				updatePc.setLevel(getLevel(updatePc.getTreePath())); // 更新Level
				organizationClassService.updateOrganizationClass(updatePc);
				if (updatePc.getLowClass().size() > 0) {
					updateTreePathAndLevel(updatePc, organizationClassService);
				}
			}
		}
	}

	// 更新所有下级分类的TreePath和Level 往来单位分类
	public static void updateTreePathAndLevel(ClassBase classBase,
			IWareHouseClassService wareHouseClassService) {
		Class cb = classBase.getClass();
		WareHouseClass wc = (WareHouseClass) classBase;
		if (wc.getLowClass().size() > 0) {
			Set<WareHouseClass> wcSets = wc.getLowClass();
			Iterator<WareHouseClass> it = wcSets.iterator();
			while (it.hasNext()) {
				WareHouseClass updateWc = it.next();
				updateWc.setTreePath(getTreePath(updateWc)); // 更新TreePath
				updateWc.setLevel(getLevel(updateWc.getTreePath())); // 更新Level
				wareHouseClassService.updateWareHouseClass(updateWc);
				if (updateWc.getLowClass().size() > 0) {
					updateTreePathAndLevel(updateWc, wareHouseClassService);
				}
			}
		}
	}
}
